package com.lx.gulimall.seckill.controller;

import com.lx.common.utils.R;
import com.lx.gulimall.seckill.service.SeckillService;
import com.lx.gulimall.seckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author lixun Email:lx3027@126.com
 * @create 2021-08-21 14:40
 */
@Controller
public class SeckillController {

    @Autowired
    SeckillService seckillService;

    @GetMapping("/currentSeckillSkus")
    @ResponseBody
    public R getCurrentSeckillSkus() {

        List<SeckillSkuRedisTo> to = seckillService.getCurrentSeckillSkus();

        return R.ok().setDate(to);
    }

    @GetMapping("/sku/seckill/{skuId}")
    @ResponseBody
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId) {

        SeckillSkuRedisTo to = seckillService.getSkuSeckillInfo(skuId);

        return R.ok().setDate(to);
    }

    @GetMapping("/kill")
    public String secKill(@RequestParam("killId") String killId,
                          @RequestParam("key") String code,
                          @RequestParam("num") Integer number,
                          Model model) {
        /**
         * 成功了就返回订单号
         */
        String orderSn = seckillService.kill(killId,code,number);
        model.addAttribute("orderSn", orderSn);
        return "success";
    }


}
